package cn.s1995.sloth.bms.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.WorldSubregions;
import cn.s1995.sloth.bizs.service.WorldSubregionsService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 子区域
 *
 * @author Lex
 * @date 2024-01-18 10:20:16
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/worldSubregions")
@Tag(description = "worldSubregions", name = "BMS-子区域管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class WorldSubregionsController {

    private final WorldSubregionsService worldSubregionsService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 子区域
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_worldSubregions_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject WorldSubregions req) {
        return R.ok(worldSubregionsService.page(page, buildQueryWrapper(req)));
    }

    /**
     * 获取列表
     * @param req 子区域
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_worldSubregions_view')")
    public R getList(@ParameterObject WorldSubregions req) {
        return R.ok(worldSubregionsService.list(buildQueryWrapper(req)));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_worldSubregions_view')")
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(worldSubregionsService.getById(id));
    }

    /**
     * 新增子区域
     * @param req 子区域
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增子区域", description = "新增子区域")
    @SysLog("新增子区域")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_worldSubregions_add')")
    public R save(@RequestBody WorldSubregions req) {
        return R.ok(worldSubregionsService.save(req));
    }

    /**
     * 修改子区域
     * @param req 子区域
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改子区域", description = "修改子区域")
    @SysLog("修改子区域")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('bizs_worldSubregions_edit')")
    public R updateById(@RequestBody WorldSubregions req) {
        return R.ok(worldSubregionsService.updateById(req));
    }

    /**
     * 通过id删除子区域
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除子区域", description = "通过id删除子区域")
    @SysLog("通过id删除子区域")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_worldSubregions_del')")
    public R remove(@RequestBody Integer[] ids) {
        return R.ok(worldSubregionsService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_worldSubregions_export')")
    public List<WorldSubregions> export(WorldSubregions req, Integer[] ids) {
        return worldSubregionsService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), WorldSubregions::getId, ids));
    }

    /**
     * 构建查询条件
     * @param req
     * @return
     */
    @NotNull
    private static LambdaQueryWrapper<WorldSubregions> buildQueryWrapper(WorldSubregions req) {
        LambdaQueryWrapper<WorldSubregions> wrapper = Wrappers.lambdaQuery(WorldSubregions.class)
                .and(
                        StrUtil.isNotEmpty(req.getSearch()),
                        i -> i.like(WorldSubregions::getName, req.getSearch())
                                .or()
                                .like(WorldSubregions::getTranslations, req.getSearch())
                );
        return wrapper;
    }
}
